package com.chapter.sample.jvm;

public class StaticDeadLockMain extends Thread {

    public char flag;

    public StaticDeadLockMain(char flag) {
        this.flag = flag;
        this.setName("Thread " + flag);
    }

    @Override
    public void run() {
        try {
            Class.forName("com.chapter.sample.jvm.Static" + flag);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(getName() + "over");
    }

    public static void main(String[] args) {
        StaticDeadLockMain loadA = new StaticDeadLockMain('A');
        loadA.start();

        StaticDeadLockMain loadB = new StaticDeadLockMain('B');
        loadB.start();

    }
}

class StaticA {
    static {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
        try {
            Class.forName("com.chapter.sample.jvm.StaticB");
        } catch (ClassNotFoundException e) {

        }
        System.out.println("StaticA init OK");
    }

}

class StaticB {
    static {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {

        }
        try {
            Class.forName("com.chapter.sample.jvm.StaticA");

            Class clazz = ClassLoader.getSystemClassLoader().loadClass("Java.lang.String");

        } catch (ClassNotFoundException e) {

        }
        System.out.println("StaticB init OK");
    }
}
